package timing.ukulele.form.mapper;

import timing.ukulele.form.persistent.FormTemplate;
import timing.ukulele.persistent.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface FromTemplateMapper extends BaseMapper<FormTemplate> {

    @Select("SELECT MAX(version) FROM c_form_template WHERE id_ = #{id} OR use_id = #{id}")
    Integer getMaxVersion(@Param(value = "id")Long id);

    @Update("UPDATE c_form_template SET use_id = #{newId} WHERE id_ = #{oldId} OR use_id = #{oldId}")
    Integer updateUseId(@Param(value = "oldId") Long oldId, @Param(value = "newId") Long newId);

    @Select("SELECT * FROM v_form_template_union WHERE id_ = #{id}")
    FormTemplate getTemplate(@Param(value = "id")Long id);

    @Select("SELECT * FROM v_form_template_union WHERE use_id = #{useId}")
    List<FormTemplate> getAllTemplate(@Param(value = "useId")Long useId);
}
